# $Id$
# Contributor: Jeff Brodnax <tullyarcher@bellsouth.net>
# Contributor: Paul Mattal <paul@archlinux.org>
# Maintainer: Gaetan Bisson <bisson@archlinux.org>

pkgname=postfix
pkgver=3.2.3
pkgrel=1
url='http://www.postfix.org/'
pkgdesc='Fast, easy to administer, secure mail server'
license=('custom')
arch=('i686' 'x86_64')
depends=('openssl' 'pcre' 'icu' 'libsasl' 'libldap' 'db'
         'libmariadbclient' 'postgresql-libs' 'sqlite' 'tinycdb')
source=("ftp://ftp.porcupine.org/mirrors/postfix-release/official/${pkgname}-${pkgver}.tar.gz"
        'aliases.patch'
        'service')
sha256sums=('5b0b975d075ea7561028d4109c581549b794aa92d733429ea6d9fa57751140bf'
            'f4c766efc20b4638f9fd72707ca6d4c2628279ebd79f5227276fa4ca6867c336'
            '52781649c49a50cecd7d20f693f602d7dc78b985ac5f0e72c4abfa0d800e58f0')

backup=('etc/postfix/'{access,aliases,canonical,generic,header_checks,main.cf,master.cf,relocated,transport,virtual})

conflicts=('smtp-server' 'smtp-forwarder' 'postfix-tools')
provides=('smtp-server' 'smtp-forwarder' 'postfix-tools')
replaces=('postfix-tools')

install=install

build() {
	cd "${srcdir}/${pkgname}-${pkgver}"

	CCARGS='
                -DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/include/sasl
                -DHAS_LDAP
                -DUSE_TLS
                -DHAS_MYSQL -I/usr/include/mysql
                -DHAS_PGSQL -I/usr/include/postgresql
                -DHAS_SQLITE
                -DHAS_CDB
                -DDEF_COMMAND_DIR=\"/usr/bin\"
                -DDEF_DAEMON_DIR=\"/usr/lib/postfix/bin\"
                -DDEF_SENDMAIL_PATH=\"/usr/bin/sendmail\"
                -DDEF_README_DIR=\"/usr/share/doc/postfix\"
                -DDEF_MANPAGE_DIR=\"/usr/share/man\"
        '
	AUXLIBS='
                -lsasl2
                -lldap -llber
                -lssl -lcrypto
                -lmysqlclient -lz -lm
                -lpq
                -lsqlite3 -lpthread
                -lcdb
        '
	make makefiles \
		DEBUG='' \
		pie=yes \
		shared=yes \
		CCARGS="${CCARGS//$'\n'/}" \
		AUXLIBS="${AUXLIBS//$'\n'/}" \
		OPT="${CFLAGS} ${LDFLAGS}" \

	make
}

package() {
	cd "${srcdir}/postfix-${pkgver}"

	sed 's:bin/postconf -dhx:bin/postconf -c conf -dhx:g' -i postfix-install
	LD_LIBRARY_PATH=lib:$LD_LIBRARY_PATH \
	sh postfix-install -non-interactive install_root="${pkgdir}"

	install -Dm644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
	install -Dm644 ../service "${pkgdir}/usr/lib/systemd/system/${pkgname}.service"

	for name in posttls-finger {smtp,qmqp}-{sink,source}; do
		install -Dm644 "man/man1/${name}.1" "${pkgdir}/usr/share/man/man1/${name}.1"
		install -Dm755 "bin/${name}" "${pkgdir}/usr/bin/${name}"
	done

	# now set permissions right to appease the pacman gods
	sed -r \
		-e '/override=1/d' \
		-e '/absolute path name/d' \
		-e 's/\$POSTCONF -/$POSTCONF -c .\/etc\/postfix -/g' \
		-e '/(if \[|chmod|chown|find|mkdir|test)/s/\$path/.\/$path/g' \
		libexec/post-install > ../arch-post-install
	cd "${pkgdir}"
	LD_LIBRARY_PATH=usr/lib/postfix:$LD_LIBRARY_PATH \
	sh "${srcdir}/arch-post-install" \
                command_directory=usr/bin \
                config_directory=etc/postfix \
                meta_directory=etc/postfix \
                setgid_group=75 \
                mail_owner=73 \
                set-permissions

	patch --no-backup-if-mismatch -p0 -i "${srcdir}"/aliases.patch
	sed 's/^\(\$manpage[^:]*\):/\1.gz:/' -i "etc/postfix/postfix-files"
}
